import axios from 'axios';
import QS from 'qs';
// let baseURL = "http://192.168.0.104:8000"
// let baseURL = "http://localhost:8000"
let baseURL = "http://47.106.157.212:8000"
// 超时时间
axios.defaults.timeout = 25000;
//请求头设置
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.baseURL = baseURL;
//import store from '../store/index'
// import { Loading, Message } from 'vant'
import router from '../router.js'

// headers = {
    // 'Authorization':'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjA3NDk5MDczLCJqdGkiOiIwNTI1Yzk0ZWFmNzM0NmQyYjFjZjg3MTA3Njc0ODk1MCIsInVzZXJfaWQiOjR9.Xpk0z4bGLOeDTjimI9j4IV8nEMfhRJykToNphtUK0Tc'
// }

let loading

// function startLoading() {
//     loading = Loading.service({
//         lock: true,
//         text: '加载中....',
//         background: 'rgba(0, 0, 0, 0.7)'
//     })
// }

// function endLoading() {
//     loading.close()
// }

// 请求拦截
axios.interceptors.request.use(
    (confing) => {
        // startLoading()
        //设置请求头
        let access_token = sessionStorage.getItem('access_token')
        if (access_token) {
            confing.headers.Authorization = 'JWT '+access_token
        }
        return confing
    },
    (error) => {
        return Promise.reject(error)
    }
)

//响应拦截
axios.interceptors.response.use(
    (response) => {
        // endLoading()
        if (response.status.toString().substring(0,1) === "2") {
            return Promise.resolve(response);
        } else {
            Message.error(JSON.stringify(response.data))
            return Promise.reject(response.data);
        }
    },
    (error) => {
        // endLoading()
        if (error.response.status) {
            console.log(error.response)
            switch (error.response.status) {
                // 401: 未登录
                case 401:
                    // Message.error('请重新登录')
                    localStorage.removeItem('eToken') //清楚token
                    router.push('/login') //跳转到登录页面
                break;
                // 403 token过期
                case 403:
                    // Message.error('登录过期，请重新登录')
                    localStorage.removeItem('eToken') //清楚token
                    router.push('/login') //跳转到登录页面
                break;
                // 404请求不存在
                case 404:
                    Message.error('网络请求不存在')
                break;
                // 其他错误，直接抛出错误提示
                default:
                    // Message.error(JSON.stringify(error.response.data))
                    console.log(error.response.data)
                    // Message.error(error.response.data)
            }
        }
        return Promise.reject(error.response);
    }
)
function _get(url, params){
    return axios.get(url, {params: params}).then(res => res.data).catch(err => {});
}
function _delete(url, params){
    return axios.delete(url, {params: params}).then(res => res.data).catch(err => {});
}
function _post(url, params){
    return axios.post(url, params).then(res => res.data).catch(err => {});
}
function _put(url, params){
    return axios.put(url, params).then(res => res.data).catch(err => {});
}
function _patch(url, params){
    return axios.patch(url, params).then(res => res.data).catch(err => {});
}

export {_get, _delete, _post, _put, _patch};
